package nju.software.xkxt.service.implement;

import java.util.ArrayList;
import java.util.List;

import nju.software.xkxt.data.dao.ScoreDao;
import nju.software.xkxt.data.dao.UserDao;
import nju.software.xkxt.data.dataobject.ScoreDO;
import nju.software.xkxt.service.ScoreService;
import nju.software.xkxt.service.model.ScoreModel;

public class ScoreServiceImpl implements ScoreService {

	ScoreDao scoreDao;
	UserDao userDao;

	public void setScoreDao(ScoreDao scoreDao) {
		this.scoreDao = scoreDao;
	}

	@Override
	public List<ScoreModel> getScoreModelsByCidAndTid(int cid, int tid) {
		List<ScoreModel> scoreModels = new ArrayList<ScoreModel>();
		List<ScoreDO> scoreDOs = scoreDao.getsScoreDOsByCidAndTid(cid, tid);
		for (ScoreDO scoreDO : scoreDOs) {
			ScoreModel scoreModel = new ScoreModel(scoreDO);
			scoreModels.add(scoreModel);
		}
		return scoreModels;
	}

	@Override
	public int addScore(ScoreModel model) {
		int maxbh = scoreDao.getMaxSid();
		maxbh++;
		ScoreDO scoreDO = new ScoreDO(model);
		scoreDO.setSId(maxbh);
		int state = scoreDao.addScore(scoreDO);
		return state;
	}

	@Override
	public boolean updateScore(List<ScoreModel> models) {
		List<ScoreDO> scoreDOs = new ArrayList<ScoreDO>();
		for (ScoreModel model : models) {
			ScoreDO scoreDO = new ScoreDO(model);
			scoreDOs.add(scoreDO);
		}
		return scoreDao.updateScore(scoreDOs);
	}

	@Override
	public ScoreModel getScoreByUidAndCid(int uid, int cid) {
		ScoreDO scoreDO = scoreDao.getScoreByUidAndCid(uid, cid);
		ScoreModel scoreModel = new ScoreModel(scoreDO);
		return scoreModel;
	}

	@Override
	public int addScore(List<ScoreModel> models) {
		int state=1;
		for (ScoreModel src : models) {
			state = addScore(src);
			if (state == 2 || state == 0)
				break;
		}
		return state;
	}

}
